package com.ymttest.database.mongo;

import java.util.ArrayList;
import java.util.Map;

import com.mongodb.WriteResult;
import com.ymt.database.MongoDBDriver;

public class YmtShoppingCartWapper {
	private MongoDBDriver mdd = YmtMongoDriverFactroy.getInstance_YmtShoppingCartDBdriverNewJava("YmtShoppingCart");
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<String,Object> getByShoppingCartId(String shoppingCartId) throws Exception {
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"_id\":\"" + shoppingCartId + "\"}",1);
	    
	    return list.size()>0?list.get(0):null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<String,Object> getByProductIdAndUserId(String productId, int userId) throws Exception {
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"productid\":\"" + productId + "\", \"userid\":" + userId + "}",1);
	    
	    return list.size()>0?list.get(0):null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<String,Object> getByCatalogIdIdAndUserId(String catalogid, int userId) throws Exception {
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"catalogid\":\"" + catalogid + "\", \"userid\":" + userId + "}",1);
	    
	    return list.size()>0?list.get(0):null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map<String,Object> getByCatalogIdIdAndUserIdAndSource(String catalogid, int userId, int source) throws Exception {
		ArrayList<Map> list = mdd.selectPartLimitToMap(
				"{\"catalogid\":\"" + catalogid + "\", \"userid\":" + userId + ",\"source\":" + source + "}",1);
	    
	    return list.size()>0?list.get(0):null;
	}
	
	public int getCountByUserId(int userId) throws Exception {
		int count = mdd.selectCount("{\"userid\":" + userId + "}");
		
	    return count;
	}
	
	//返回为0，未找到待删除的数据
	public int deleteByProductIdAndUserId(String productId, int userId) throws Exception {
		WriteResult result = mdd.delete("{\"productid\":\"" + productId + "\", \"userid\":" + userId + "}");
		
	    return result.getN();
	}
	
	//返回为0，未找到待删除的数据
	public int deleteByUserId(int userId) throws Exception {
		WriteResult result = mdd.delete("{\"userid\":" + userId + "}");
		
	    return result.getN();
	}
	
	public static void main(String[] args) throws Exception {
		int n = new YmtShoppingCartWapper().getCountByUserId(32142);
		
		System.out.println(n);
	}
}
